package constant.milk.toilet.dialog;

import constant.milk.toilet.R;
import constant.milk.toilet.util.MilkUtil;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MilkStickerDialog extends Dialog
{
	private Context context;
	private float TEXT_SIZE;
	
	private StickerCollBank collbank = null;
	
	private View contentView;
	
	private View sticker1;
	private View sticker2;
	private View sticker3;
	private View sticker4;
	private View sticker5;
	private View sticker6;
	private View sticker7;
	private View sticker8;
	private View sticker9;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dialog_sticker);
		
		setSettingFontSize();
		
		setFindView();
		setOnClick();
		setData();
	}
	
	private void setFindView()
	{
		contentView = findViewById(R.id.dialogStickerContentView);
		sticker1 = findViewById(R.id.dialogSticker1View);
		sticker2 = findViewById(R.id.dialogSticker2View);
		sticker3 = findViewById(R.id.dialogSticker3View);
		sticker4 = findViewById(R.id.dialogSticker4View);
		sticker5 = findViewById(R.id.dialogSticker5View);
		sticker6 = findViewById(R.id.dialogSticker6View);
		sticker7 = findViewById(R.id.dialogSticker7View);
		sticker8 = findViewById(R.id.dialogSticker8View);
		sticker9 = findViewById(R.id.dialogSticker9View);
	}

	private void setOnClick()
	{
		contentView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getSticker(0);
				dismiss();
			}
		});
		
		sticker1.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getSticker(1);
				dismiss();
			}
		});
		
		sticker2.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getSticker(2);
				dismiss();
			}
		});
		
		sticker3.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getSticker(3);
				dismiss();
			}
		});
		
		sticker4.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getSticker(4);
				dismiss();
			}
		});
		
		sticker5.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getSticker(5);
				dismiss();
			}
		});
		
		sticker6.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getSticker(6);
				dismiss();
			}
		});
		
		sticker7.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getSticker(7);
				dismiss();
			}
		});
		
		sticker8.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getSticker(8);
				dismiss();
			}
		});
		
		sticker9.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getSticker(9);
				dismiss();
			}
		});
	}
	
	@Override
	public void onBackPressed()
	{
		collbank.getSticker(0);
		super.onBackPressed();
	}

	private void setData()
	{
		ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
		setGlobalFont(root);
	}

	public MilkStickerDialog(Context context, StickerCollBank collBank)
	{
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		
		this.context = context;
		this.collbank = collBank;
	}
	
	private void setSettingFontSize()
	{
		int fontSize = 9;
		
		SharedPreferences p = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int fontFlag = p.getInt(context.getString(R.string.prefFont), 1);
		
		if (fontFlag == 0) fontSize = 13;
		else if (fontFlag == 1) fontSize = 16;
		else if (fontFlag == 2) fontSize = 20;
		
		TEXT_SIZE = fontSize;
	}
	
	private void setGlobalFont(ViewGroup root) 
	{
	    for (int i = 0; i < root.getChildCount(); i++) 
	    {
	        View child = root.getChildAt(i);
	        
	        if (child instanceof TextView)
	        {
	        	((TextView)child).setTextColor(Color.BLACK);
	        	((TextView)child).setTextSize(TEXT_SIZE);
	        }
	        else if (child instanceof EditText)
			{
	        	((EditText)child).setTextColor(Color.BLACK);
	        	((EditText)child).setTextSize(TEXT_SIZE);
			}
	        else if (child instanceof Button)
			{
	        	((Button)child).setTextColor(Color.BLACK);
	        	((Button)child).setTextSize(TEXT_SIZE);
			}
	        else if (child instanceof ViewGroup)
	        {
	        	setGlobalFont((ViewGroup)child);
	        }
	    }
	}
	
	public interface StickerCollBank
	{
		public abstract void getSticker(int sticker);
	}
}
